package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.Teacher;
import com.atguigu.eduservice.entity.ov.TeacherQuery;
import com.atguigu.eduservice.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-18
 */
@RestController
@RequestMapping("/eduservice/teacher")
@Api(description = "讲师的增删改")
//@CrossOrigin
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/getTeacher")
    public R getTeacher(){
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    //逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name="id",value = "讲师id",required = true) @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if(flag==false){
            return R.error();
        }else {
            return R.ok();
        }
    }


    //4 条件查询带分页的方法
    @PostMapping("pageTeacher/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,@PathVariable long limit,
                                  @RequestBody(required = false)  TeacherQuery teacherQuery) {
        //创建page对象
        Page<Teacher> pageTeacher = new Page<>(current,limit);

        //构建条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(level!=0) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("createtime",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("createtime",end);
        }

        //排序
        wrapper.orderByDesc("createtime");

        //调用方法实现条件查询分页
        teacherService.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<Teacher> records = pageTeacher.getRecords(); //数据list集合
        return R.ok().data("total",total).data("list",records);
    }

    @ApiOperation(value = "添加教师")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if(save==true){
            return R.ok();
        }else{
            return R.error();
        }

    }

    @ApiOperation(value="根据id查询教师")
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return  R.ok().data("teacher",teacher);
    }

    @ApiOperation(value = "修改对应的教师")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody Teacher teacher){
        boolean flag = teacherService.updateById(teacher);
        if(flag==true){
            return  R.ok();
        }else{
            return  R.error();
        }
    }


    @PostMapping("/login")
    public R login (){ 
        return R.ok().data("token","admin");
    }

    @GetMapping("/info")
    public R info(){
        return R.ok().data("roles","[admin]").data("name","admin").data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }
}

